package tw.twandroidproj.app.ui;

import android.content.Context;
import android.media.Image;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import tw.twandroidproj.app.R;



/**
 * Created by Raymon on 2016/12/2.
 */

public class BadgeActionProvider extends ActionProvider {
    /**
     * Creates a new instance. ActionProvider classes should always implement a
     * constructor that takes a single Context parameter for inflating from menu XML.
     *
     * @param context Context for accessing resources.
     */
    private ImageView sms_imageview;
    private TextView sms_textview;
    public BadgeActionProvider(Context context) {
        super(context);
    }

    private OnClickListener onClickListener;
    @Override
    public View onCreateActionView() {
        int size = getContext().getResources().getDimensionPixelSize(
                android.support.design.R.dimen.abc_action_bar_default_height_material);

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(size, size);
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.view_badgeactionprovider, null, false);

        view.setLayoutParams(layoutParams);
        sms_imageview = (ImageView) view.findViewById(R.id.sms_icon);
        sms_textview = (TextView) view.findViewById(R.id.sms_badge);
        view.setOnClickListener(onViewClickListener);
        return view;
    }

    // 点击处理。
    private View.OnClickListener onViewClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onClickListener != null)
                onClickListener.onClick();
        }
    };

    // 外部设置监听。
    public void setOnClickListener( OnClickListener onClickListener) {

        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onClick();
    }
    /**
     * 设置图标。
     *
     * @param icon drawable 或者mipmap中的id。
     */
    public void setIcon(@DrawableRes int icon) {
        sms_imageview.setImageResource(icon);
    }

    /**
     * 设置显示的数字。
     *
     * @param i 数字。
     */
    public void setBadge(String i) {
        sms_textview.setVisibility(View.VISIBLE);
        sms_textview.setText(i);
    }
    public void settextfail(){
        sms_textview.setVisibility(View.GONE);
    }

}
